<template>
  <div style="margin-top: 20px">
    <el-table v-loading="loading" :data="processList" :span-method="objectSpanMethod">
      <el-table-column label="工序名称" align="center" prop="processName"/>
      <!--      <el-table-column label="任务编号" width="100px" align="center" prop="taskCode" class-name="noprint"/>
            <el-table-column label="任务名称" width="150px" align="center" prop="taskName" class-name="noprint"/>-->
      <el-table-column label="排产数量" align="center" prop="quantity"/>
      <el-table-column label="已生产数量" width="100px" align="center"/>
      <!--      <el-table-column label="待检数量" align="center" class-name="noprint"/>-->
      <!--      <el-table-column label="工作站编号" width="100px" align="center" prop="workstationCode" class-name="noprint"/>-->
      <el-table-column label="工作站" width="100px" align="center" prop="workstationName"/>
      <!--      <el-table-column label="计划开工时间" width="100px" align="center" prop="startTime" class-name="noprint"/>-->
      <el-table-column label="生产时长" align="center" prop="duration"/>
      <!--      <el-table-column label="计划完工时间" width="100px" align="center" prop="endTime" class-name="noprint"/>-->
      <el-table-column label="增补工时" align="center" prop="actualDuration"/>
      <el-table-column label="实际工时" align="center" prop="actualDuration"/>
      <el-table-column label="返工原因" align="center">
        <el-table-column label="设计" width="40"/>
        <el-table-column label="外协" width="40"/>
        <el-table-column label="采购" width="40"/>
        <el-table-column label="其他" width="40"/>
      </el-table-column>
      <el-table-column label="质检员" align="center"/>
      <el-table-column label="备注" align="center"/>
    </el-table>
  </div>
</template>

<script>
import { listProtask } from "@/api/mes/pro/protask";

export default {
  name: "ProcessSelectSingle",
  props: {
    workorderCode: null,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 过程检验单行表格数据
      processList: [],
      spanArr: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10000,
        workorderCode: this.workorderCode,
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询过程检验单行列表 */
    getList() {
      this.loading = true;
      listProtask(this.queryParams).then(response => {
        this.processList = response.rows;
        this.total = response.total;
        this.loading = false;
        // 优化后的 spanArr 计算逻辑
        this.spanArr = [];
        this.pos = 0;

        this.processList.forEach((item, index) => {
          if (index === 0 || item.processId !== this.processList[ index - 1 ].processId) {
            // 当前行是第一个或者与前一行 processId 不同
            this.spanArr.push(1);
            this.pos = index; // 更新 pos
          } else {
            // 当前行与前一行 processId 相同
            this.spanArr[ this.pos ] += 1;
            this.spanArr.push(0); // 当前行合并单元格数为 0
          }
        });
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    objectSpanMethod({row, column, rowIndex, columnIndex}) {
      if (columnIndex === 0) {
        const _row = this.spanArr[ rowIndex ];
        const _col = _row > 0 ? 1 : 0;
        return {
          rowspan: _row,
          colspan: _col
        }
      }
    },
  }
};
</script>

